package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import entity.Attence;

public class AttenceDao extends BaseDao{
	
	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	
	public List<Attence> Sele(){
		List<Attence> list=new ArrayList<Attence>();
		try {
			conn=super.getConnection();
			String sql="select * from attence order by chkDate desc";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while (rs.next()) {
				Attence attence=new Attence();
				attence.setId(rs.getInt("id"));
				attence.setEmpName(rs.getString("empName"));
				attence.setDept(rs.getString("dept"));
				attence.setChkDate(rs.getString("chkDate"));
				attence.setStatus(rs.getString("status"));
				list.add(attence);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}
	
	public int Add(Attence attence){
		int result=0;
		try {
			conn=super.getConnection();
			String sql="insert into attence(empName,dept,chkDate,status) values(?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1,attence.getEmpName());
			pstmt.setObject(2,attence.getDept());
			pstmt.setObject(3,attence.getChkDate());
			pstmt.setObject(4,attence.getStatus());
			result=pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}
	
	public int  Dele(int id){
		int result=0;
		try {
			conn=super.getConnection();
			String sql="delete from attence where id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1,id);
			result=pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}

}
